package com.platform.modules.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.platform.common.annotation.SysLog;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.Constant;
import com.platform.common.utils.RestResponse;
import com.platform.modules.sys.entity.SmsConfig;
import com.platform.modules.sys.entity.SysSmsLogEntity;
import com.platform.modules.sys.service.SysConfigService;
import com.platform.modules.sys.service.SysSmsLogService;

/**
 * 短信发送日志Controller
 *
 * @author 肖亮
 */
@Api(tags = "SysSmsLogController|短信发送日志管理")
@RestController
@RequestMapping("sys/smslog")
public class SysSmsLogController extends AbstractController {
	@Autowired
	private SysSmsLogService sysSmsLogService;
	@Autowired
	private SysConfigService sysConfigService;

	/**
	 * 查看所有列表
	 *
	 * @param params
	 *            查询参数
	 * @return RestResponse
	 */
	@ApiOperation(value = "查看所有列表", notes = "查看所有列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "sendId", value = "发送编号", dataType = "String")
    })
	@PostMapping("/queryAll")
	@RequiresPermissions("sys:smslog:list")
	public RestResponse queryAll(String sendId) {
		
		Map<String, Object> params = new HashMap<String, Object>();
			params.put("sendId", sendId);
		List<SysSmsLogEntity> list = sysSmsLogService.queryAll(params);

		return RestResponse.success().put("list", list);
	}

	/**
	 * 分页查询
	 *
	 * @param params
	 *            查询参数
	 * @return RestResponse
	 */
	@ApiOperation(value = "查看所有列表-分页查询", notes = "查看所有列表-分页查询")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "sendId", value = "发送编号", dataType = "String")
    })
	@PostMapping("/list")
	@RequiresPermissions("sys:smslog:list")
	public RestResponse list(Integer currPage,Integer limit,String sendId) {
		Map<String, Object> params = new HashMap<String, Object>();
			params.put("page", PageUtil.getCurrPage(currPage));
			params.put("limit", PageUtil.getLimit(limit));
			params.put("sendId", sendId);
		IPage page = sysSmsLogService.queryPage(params);
		
		return RestResponse.success().put("page", page);
	}

	/**
	 * 根据主键查询详情
	 *
	 * @param id
	 *            主键
	 * @return RestResponse
	 */
	@ApiOperation(value = "根据主键查询详情", notes = "根据主键查询详情")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "id", value = "发送编号", dataType = "String")
    })
	@PostMapping("/info/{id}")
	@RequiresPermissions("sys:smslog:info")
	public RestResponse info(String id) {
		SysSmsLogEntity sysSmsLog = sysSmsLogService.getById(id);

		return RestResponse.success().put("smslog", sysSmsLog);
	}

	/**
	 * 保存
	 *
	 * @param sysSmsLog
	 *            sysSmsLog
	 * @return RestResponse
	 */
	@ApiOperation(value = "保存短信发送记录", notes = "保存短信发送记录")
	@ApiImplicitParams({
		
    })
	@SysLog("保存短信发送记录")
	@PostMapping("/save")
	@RequiresPermissions("sys:smslog:save")
	public RestResponse save(SysSmsLogEntity sysSmsLog) {

		sysSmsLogService.add(sysSmsLog);

		return RestResponse.success();
	}

	/**
	 * 修改
	 *
	 * @param sysSmsLog
	 *            sysSmsLog
	 * @return RestResponse
	 */
	@ApiOperation(value = "修改短信发送记录", notes = "修改短信发送记录")
	@SysLog("修改短信发送记录")
	@PostMapping("/update")
	@RequiresPermissions("sys:smslog:update")
	public RestResponse update(@RequestBody SysSmsLogEntity sysSmsLog) {

		sysSmsLogService.update(sysSmsLog);

		return RestResponse.success();
	}

	/**
	 * 删除
	 *
	 * @param ids
	 *            ids
	 * @return RestResponse
	 */
	@ApiOperation(value = "删除角色", notes = "根据id删除模型（支持多id删除）")
	@SysLog("删除短信发送记录")
	@PostMapping("/delete")
	@RequiresPermissions("sys:smslog:delete")
	public RestResponse delete(String[] ids) {
		sysSmsLogService.deleteBatch(ids);

		return RestResponse.success();
	}

	/**
	 * 短信配置信息
	 *
	 * @return RestResponse
	 */
	@ApiOperation(value = "短信配置信息", notes = "短信配置信息")
	@PostMapping("/config")
	@RequiresPermissions("sys:smslog:config")
	public RestResponse config() {
		SmsConfig config = sysConfigService.getConfigObject(Constant.SMS_CONFIG_KEY, SmsConfig.class);

		return RestResponse.success().put("config", config);
	}

	/**
	 * 保存短信配置信息
	 *
	 * @param config
	 *            config
	 * @return RestResponse
	 */
	@ApiOperation(value = "保存短信配置信息", notes = "保存短信配置信息")
	@SysLog("保存短信配置信息")
	@RequiresPermissions("sys:smslog:config")
	@PostMapping("/saveConfig")
	public RestResponse saveConfig(SmsConfig config) {
		sysConfigService.updateValueByKey(Constant.SMS_CONFIG_KEY, JSON.toJSONString(config));
		return RestResponse.success();
	}

	/**
	 * 发送短信
	 *
	 * @param smsLog
	 *            smsLog
	 * @return RestResponse
	 */
	@ApiOperation(value = "系统调用发送短信", notes = "系统调用发送短信")
	@SysLog("系统调用发送短信")
	@RequiresPermissions("sys:smslog:send")
	@PostMapping("/sendSms")
	@ResponseBody
	public RestResponse sendSms(SysSmsLogEntity smsLog) {
		SysSmsLogEntity sysSmsLogEntity = sysSmsLogService.sendSms(smsLog);
		return RestResponse.success().put("result", sysSmsLogEntity);
	}
}
